package com.sh.data.engine.common.util;

import org.apache.commons.lang3.StringUtils;

/**
 * @author tinglan.ys
 * @description:
 * @date 2022/5/19 上午9:41
 */
public class TableNameUtil {

    /**
     * @description : 拼接带schema的表名
     * @author : tinglan.ys
     * @date : 2022/12/20
     */
    public static String getFullNameName(String schema, String tblName) {
        if (StringUtils.isBlank(schema)) {
            return tblName;
        } else {
            return schema + "." + tblName;
        }
    }

    /**
     * @description : 获取不带schema的表名
     * @author : tinglan.ys
     * @date : 2022/5/19
     */
    public static String getTblNameShort(String tblName) {
        if (tblName.contains(".")) {
            int index = tblName.indexOf(".");
            tblName = tblName.substring(index + 1);
        }
        return tblName;
    }

    /**
     * @description : 对表名加上双引号 "public"."ads_manu_f_procurementplan_d"
     * @author : tinglan.ys
     * @date : 2022/5/19
     */
    public static String getTblNameLong(String tblName) {

        String tableName;
        String schema;
        if (tblName.contains(".")) {
            int index = tblName.indexOf(".");
            tableName = tblName.substring(index + 1);
            schema = tblName.substring(0, index);
            return "\"" + schema + "\".\"" + tableName + "\"";
        }
        return "\"" + tblName + "\"";
    }

    public static String getTblSchema(String tblName) {
        String schema = "";
        if (tblName.contains(".")) {
            int index = tblName.indexOf(".");
            schema = tblName.substring(0, index);
        }
        return schema;
    }

    public static void main(String[] args) {
        String code = "public.ads_manu_f_procurementplan_d";
        System.out.println("before: " + code);
        if (code.contains(".")) {
            int index = code.indexOf(".");
            code = code.substring(index + 1);
        }
        System.out.println("after: " + code);

        String code2 = "public.ads_manu_f_procurementplan_d";
        System.out.println("before: " + code2);
        if (code2.contains(".")) {
            int index = code2.indexOf(".");
            code2 = code2.substring(0, index);
        }
        System.out.println("after: " + code2);

        System.out.println(getTblNameLong("public.ads_manu_f_procurementplan_d"));
    }
}
